/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package kernel;

import GUI.GraphicalUserInterface;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;




/**
 *
 * @author coco
 */
public class Login {
public static String Login(String ident, String psswd){
        Connection conn = null; //initialisation de la connexion
        try {


            conn=kernel.ConnectDB.ConnectDB();
            String log="call control_login("+ ident.replace("'","''")+","+psswd +",@session)"; 
            CallableStatement call = conn.prepareCall(log);
           

            String ID = ("0");
            String sessionid = null;
            int userid;
            CallableStatement stmt = conn.prepareCall("call control_login(?,?,?,?)");
            //  set the in param
            stmt.setString(1, ident.replace("'","''"));
            stmt.setString(2, psswd);
            //set the out param
            int  error;

            stmt.registerOutParameter(3, 4);
		
            ResultSet rs = stmt.executeQuery();
            ID = stmt.getString(3);
            error=stmt.getInt(4);

            if(error!=3){
            stmt = conn.prepareCall("call Datum_uhr_login(?,?,?)");
            //  set the in para
            stmt.setString(1, ID);
            //set the out param
            
            stmt.registerOutParameter(2, 3);
            rs = stmt.executeQuery();
            sessionid = stmt.getString(2);
            userid=stmt.getInt(3);
            GUI.LoginWindow.sessionID=ID;
                        stmt = conn.prepareCall("call user_privi(?,?,?)");
            //  set the in para
            stmt.setInt(1, userid);
            //set the out param

            stmt.registerOutParameter(2, 3);
            rs = stmt.executeQuery();
           String username = stmt.getString(2);
            String privi=stmt.getString(3);
            GUI.LoginWindow.benutzername=username;
            //STORING THE PRIVILEGES FOR THE LOGGED USER!
            if(privi.contains("CanSeeHistories")) GUI.LoginWindow.CanSeeHistories=true;
            if(privi.contains("CanSeeStatistics")) GUI.LoginWindow.CanSeeStatistics=true;
            if(privi.contains("CanManageUsers")) GUI.LoginWindow.CanManageUsers=true;
            if(privi.contains("CanEditCarData")) GUI.LoginWindow.CanEditCarData=true;
            System.out.println(privi);
            
            //OLD WAY
            //GUI.LoginWindow.privilegien=privi;
            return(sessionid);
            }
              return(String.valueOf(error));
            
            
        } catch (SQLException e) {
            e.printStackTrace();
            return(null);
       }
       
    }


    public static int Logout(String ident){
        Connection conn = null; //initialisation de la connexion
        try {


            conn=kernel.ConnectDB.ConnectDB();

            String sessionid = null;
            int userid;
            CallableStatement stmt = conn.prepareCall("call control_logout(?,?)");
            System.out.println(stmt);
            //  set the in param
            stmt.setString(1,ident);
            //set the out param
            int  error;

            stmt.registerOutParameter(2, 2);

            ResultSet rs = stmt.executeQuery();
            error=stmt.getInt(2);

            return(error);


        } catch (SQLException e) {
            e.printStackTrace();
            return(0);
       }

    }
}

